package mapSystem;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import tools.Constants;
import tools.GameElement;
import tools.ImageHandler;

import mapSystem.interaction.*;


public abstract class TileHandler {
	
	public static Trap procTrap(int type, int dir, BufferedImage image, int x, int y) throws IOException{
		
		/* Traps:      
		 * 			   can be self activated?
		 * 			   | player can activate?
		 * {		   | |
		 * 01:1x1>1:01>1:0
		 *  |  |  |  |
		 *  |  |  |  imgTag
		 *  |  |  vel
		 *  |  dimensions
		 *  trapID
		 * }
		 *  */
		
		InputStream iS = TileHandler.class.getResourceAsStream("/mapSystem/interaction/actionList");
		if(iS == null) return null;
		
		InputStreamReader iR = new InputStreamReader(iS);
		BufferedReader reader = new BufferedReader(iR);
		
		String curLine = reader.readLine();
		
		while(!curLine.contains("traps") && curLine != null)
		{
			curLine = reader.readLine();
		}
		if(curLine == null) return null;
		
		curLine = reader.readLine();
		
		int id = -1;
		String dim = null;
		int w = 0;
		int h = 0;
		int vel = 0;
		int imgTag = 0;
		BufferedImage img = null;
		GameElement element = null;
		boolean exists = false;
		int selfActivatable = 0;
		int playerActivate = 0;
		
		while(!curLine.equals("}") && curLine != null)
		{
			curLine = reader.readLine();
			
			if(!curLine.equals("}") && curLine != null)
			{
				id = Integer.parseInt(curLine.split(":")[0]);
				if(id == type)
				{
					exists = true;
					curLine = curLine.replaceFirst(id+":", "");
					dim = curLine.split(">")[0];
					w = Integer.parseInt(dim.split("x")[0]);
					h = Integer.parseInt(dim.split("x")[1]);

					curLine = curLine.replace(dim+">", "");
					vel = Integer.parseInt(curLine.split(":")[0]);

					curLine = curLine.replaceFirst(vel+":", "");
					imgTag = Integer.parseInt(curLine.split(">")[0])-1;
					img = ImageHandler.loadImage("/img/elements/trapElements.png");
					if(imgTag < 0 || img == null) img = new BufferedImage(w, h, BufferedImage.OPAQUE);
					else 
					{
						img = img.getSubimage((imgTag%(img.getWidth()/Constants.TILE_WIDTH))*Constants.TILE_WIDTH, 
								(imgTag/(img.getWidth()/Constants.TILE_WIDTH))*Constants.TILE_HEIGHT, 
								Constants.TILE_WIDTH, Constants.TILE_HEIGHT);
						img = ImageHandler.addColorKey(img);
						img = img.getSubimage(0, 0, w, h);
					}
					
					curLine = curLine.substring(curLine.length()-3, curLine.length());
					selfActivatable = Integer.parseInt(curLine.split(":")[0]);
					playerActivate = Integer.parseInt(curLine.split(":")[1]);
					
					if(dir == 1) // all images take left as default
					{
						element = new GameElement(img, (x*Constants.TILE_WIDTH)-w, 
								y*Constants.TILE_HEIGHT+(Constants.TILE_HEIGHT-h)/2, 
								w, h, -1*vel, 0, -1);
					}
					if(dir == 2)
					{
						img = ImageHandler.flipHorizontally(img);
						element = new GameElement(img, (x+1)*Constants.TILE_WIDTH, 
								y*Constants.TILE_HEIGHT+(Constants.TILE_HEIGHT-h)/2, 
								w, h, vel, 0, -1);
					}
					if(dir == 3)
					{
						int aux = w;
						w = h;
						h = aux;
						
						img = ImageHandler.rotateCW(img);
						element = new GameElement(img, x*Constants.TILE_WIDTH+((Constants.TILE_WIDTH-w)/2), 
								(y*Constants.TILE_HEIGHT)-h, 
								w, h, 0, -1*vel, -1);
					}
					if(dir == 4)
					{
						int aux = w;
						w = h;
						h = aux;
						
						img = ImageHandler.rotateCCW(img);
						element = new GameElement(img, x*Constants.TILE_WIDTH+((Constants.TILE_WIDTH-w)/2), 
								(y+1)*Constants.TILE_HEIGHT, 
								w, h, 0, vel, -1);
					}
					if(dir == 0)
					{
						element = new GameElement(img, x*Constants.TILE_WIDTH, y*Constants.TILE_HEIGHT,
								w, h, 0, 0, -1);
					}
				}
			}
		}
		
		Trap t = null;
		
		/* check if given type exists */
		if(exists) t = new Trap(x*Constants.TILE_WIDTH, y*Constants.TILE_HEIGHT, 
				image, element, selfActivatable, playerActivate);
		
		return t;
	}
}
